package com.apes.fico.finance.model.gl;

import com.apes.fico.masterdata.gl.model.AccountingOperatingChart;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * @program: apestech
 * @description: 每期发生额
 * @author: Chengxm
 * @create: 2019-01-15 13:49
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ChartAmount.CONST_TABLE_NAME, indexes = {
        @Index(columnList = "year,period,company_id,acc_operating_chart_id", unique = true)
})
public class ChartAmount extends Model {
    public  final static  String CONST_TABLE_NAME = "gl_chart";
    @Id
    @Column(name = "id", nullable = false, length = 120)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "科目发生额汇总", padding = 8)
    @Comment("编号")
    private String id;

    @NotNull
    @Column
    @Comment("会计年度")
    private int year;

    @NotNull
    @Column
    @Comment("会计期间")
    private int period;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    @Comment("公司")
    private PartyRole company;

    @NotNull
    @ManyToOne()
    @JoinColumn(name = "acc_operating_chart_id", referencedColumnName = "id")
    @Comment("科目")
    private AccountingOperatingChart accountingOperatingChart;


    @Column(name = "un_debit", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("未记账借方金额")
    private double unDebit = 0;

    @Column(name = "un_credit", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("未记账贷方金额")
    private double unCredit = 0;


    @Column(name = "debit", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("借方金额")
    private double debit = 0;

    @Column(name = "credit", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("贷方金额")
    private double credit = 0;

    @Column(name = "opening_balance", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("期初余额") /*借方-贷方*/
    private double openingBalance = 0;

    public void changeUnDebit(double changeQty) {
        this.unDebit += changeQty;
    }

    public void changeUnCredit(double changeQty) {
        this.unCredit += changeQty;
    }

    public void changeDebit(double changeQty) {
        this.debit += changeQty;
    }

    public void changeCredit(double changeQty) {
        this.credit += changeQty;
    }
}
